home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: Need help on program
- Date: 4 Feb 1996 05:37:37 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4f1gn1$kup@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- phaley@saucer.cc.umr.edu (Paul Haley) in <4f0vat$j8j@hptemp1.cc.umr.edu>
- asks:
-
- > Hello. I'm trying to write a program which will count the total
- >amount of a "poker" jackpot. I read the input into an array, then read
- >the array values in, and take appropriate action.
- > c = call
- > d = drop
- > 5 players are in the game
-
- > Example input would be:
- > 5cccc
- > 5cc10cc
- > 100cc5cc
-
- > My problem with the program, is that the only time the program is
- >100% correct is when I have a single digit number followed by c's or d's.
- >Anything else will give a slightly higher answer than I need. Here is
- >the function.
-
- The problem with the code is in your use of atoi(). In the slightly
- changed code below, my changes and comments are marked with /* mha ... */.
- Some of my changes (i.e. commenting out unused vars) are to make it
- easier for me to read the code and are not crucial.
-
- #include <ctype.h> /* mha - added */
- #include <stdio.h> /* mha - added */
- void end_game(int); /* mha - added */
-
- void array_check(int i, char storage[])
- {
- int pot = 0, j = 0, temp = 0, old_call_value = 0;
- int call_value = 0, prev = 0;
- #if 0
- /* mha - unused variables commented out */
- char temp2;
- int raise = 0, num_plays = 0, c = 0, count = 0;
- #endif
-
- for (j = 0; j < i; ++j) {
- if (isdigit(storage[j]) != 0) {
- if (prev == 1) { /* prev means there was a previous
- * digit */
- #if 0
- /* mha - replaced code commented out */
- temp2 = storage[j];
- c = atoi(&temp2);
- temp = temp * 10 + c;
- #endif
- /* mha - the above use of atoi is wrong. atoi() is for
- * converting strings to ints, not for converting
- * digits to their equivalents. */
- temp = temp * 10 + storage[j] - '0'; /* mha - new */
- prev = 1;
- } else {
- #if 0
- /* mha - replaced code commented out */
- temp2 = storage[j];
- temp = atoi(&temp2);
- #endif
- temp = storage[j] - '0'; /* mha - see above comments */
- prev = 1;
- }
- old_call_value = call_value;
- puts("call_value=temp");
- call_value += temp;
-
- }
- if (storage[j] == 'c') {/* begin 'c' check */
- printf("storage[%d] = 'c'\n", j);
- old_call_value = 0;
- prev = 0;
- } /* end 'c' check */
- if (storage[j] == 'd') {/* begin 'd' check */
- old_call_value = 0;
- prev = 0;
- puts("storage[j] = d and prev is set to 0");
- }
- /* end 'd' check */
- else
- pot += (call_value - old_call_value);
- }
- end_game(pot);
- }
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-